import WXAD from "../../StartAb/WXAd";
import EventCenter from "../TFrame/script/helper/EventCenter";
import EventNames from "./EventNames";
import GameLogicController from "./game/GameLogicController";
import StatusProperties, { RewardType } from "./game/StatusProperties";
import { ActionType } from "./net/Enum";
import ServiceLogic from "./net/ServerLogic";
import PlayerDataMgr from "./PlayerDataMgr";
import IAPSystem from "./system/IAPSystem";
import ToastHelper from "./ui/ToastHelper";
//@ts-ignore
const i18n = require('LanguageData');

export default class NativeCaller {
    static init() {
        window['interstitialShown'] = NativeCaller.interstitialShown;
        window['rewardedCall'] = NativeCaller.rewardSuccess;
        window['rewardShowFailed'] = NativeCaller.rewardShowFailed;
        window['onAdPaid'] = NativeCaller.onAdPaid;
    }

    //ios第一次运行时，有网络权限弹窗，等弹窗关了,js这边运行后再加载广告
    static firstLoadAd() {
        cc.log("NativeHandler firstLoadAd")
        if (cc.sys.isNative && cc.sys.os == cc.sys.OS_IOS) {
            // jsb.reflection.callStaticMethod("NativeHandler", "firstLoadAd", null);
        }
    }

    static showFull() {
        WXAD.instance.showInterstitial()
    }

    static showReward(): boolean {
        if (cc.sys.isNative && cc.sys.os == cc.sys.OS_ANDROID) {
            // return jsb.reflection.callStaticMethod("com/common/ads/AdManage", "showReward", "()V");
        }
        if (cc.sys.isNative && cc.sys.os == cc.sys.OS_IOS) {
            // jsb.reflection.callStaticMethod("NativeHandler", "showReward", null);
        }
        if (cc.sys.os == cc.sys.OS_WINDOWS) {
            cc.Canvas.instance.scheduleOnce(this.rewardSuccess, 0.1)
            // this.rewardShowFailed();
            return false;
        }
        return false;
    }

    static showBanner(hwratio = 2): void {
        let { width, height } = cc.view.getFrameSize()
        console.log("IAD NativeCaller showBanner:", height / width)
        if ((height / width) < hwratio)
            return
        if (cc.sys.isNative && cc.sys.os == cc.sys.OS_ANDROID) {
            // return jsb.reflection.callStaticMethod("com/common/ads/AdManage", "showBannerAd", "()V");
        }
        else if (cc.sys.isNative && cc.sys.os == cc.sys.OS_IOS) {
            // jsb.reflection.callStaticMethod("NativeHandler", "showBannerAd", null);
        }
    }

    static hideBanner(): void {
        if (cc.sys.isNative && cc.sys.os == cc.sys.OS_ANDROID) {
            // return jsb.reflection.callStaticMethod("com/common/ads/AdManage", "hideBannerAd", "()V");
        } else if (cc.sys.isNative && cc.sys.os == cc.sys.OS_IOS) {
            // jsb.reflection.callStaticMethod("NativeHandler", "hideBannerAd", null);
        }
    }

    static setBannerTop(): void {
        if (cc.sys.isNative && cc.sys.os == cc.sys.OS_ANDROID) {
            // return jsb.reflection.callStaticMethod("com/common/ads/AdManage", "setBannerTop", "()V");
        } else if (cc.sys.isNative && cc.sys.os == cc.sys.OS_IOS) {
            // jsb.reflection.callStaticMethod("NativeHandler", "setBannerTop", null);
        }
    }

    static setBannerBottom(): void {
        if (cc.sys.isNative && cc.sys.os == cc.sys.OS_ANDROID) {
            // return jsb.reflection.callStaticMethod("com/common/ads/AdManage", "setBannerBottom", "()V");
        }
        else if (cc.sys.isNative && cc.sys.os == cc.sys.OS_IOS) {
            // jsb.reflection.callStaticMethod("NativeHandler", "setBannerBottom", null);
        }
    }

    static showRateus(): void {
    }

    static linkToGooglePlay(bundleId: string): void {

    }

    static showLeaderBoard(): void {
        console.log("NativeCaller showLeaderBoard");
        if (cc.sys.isNative && cc.sys.os == cc.sys.OS_ANDROID) {
            // return jsb.reflection.callStaticMethod("com/common/google/GPGameManager", "s_showLeaderBoard", "()V");
        }
        else if (cc.sys.isNative && cc.sys.os == cc.sys.OS_IOS) {
            // cc.sys.isNative && jsb.reflection.callStaticMethod("NativeHandler", "showLeaderBoard", null);
        }
    }
    static leaderboardSubmitScore(score): void {
        console.log("NativeCaller leaderboardSubmitScore");
        if (cc.sys.isNative && cc.sys.os == cc.sys.OS_ANDROID) {
            // return jsb.reflection.callStaticMethod("com/common/google/GPGameManager", "s_submitScore", "(I)V", score);
        }
        if (cc.sys.isNative && cc.sys.os == cc.sys.OS_IOS) {
            // jsb.reflection.callStaticMethod("NativeHandler", "submitLeaderBoardScore:lcId:", score, "StarLeader");
        }
    }

    static requestBuy(iapId) {
        console.log("IAD NativeCaller requestBuy:%s", iapId)
        if (cc.sys.isNative && cc.sys.os == cc.sys.OS_ANDROID) {
            // cc.sys.isNative && jsb.reflection.callStaticMethod("com/common/ads/AdManage", "requestBuy", "(Ljava/lang/String;)V", iapId);
        } else if (cc.sys.isNative && cc.sys.os == cc.sys.OS_WINDOWS) {
            cc.Canvas.instance.scheduleOnce(() => {
                window['iapBuyResult'](iapId, 1)
            }, 1)
        }
    }

    static getPackageName() {
        if (cc.sys.isNative && cc.sys.os == cc.sys.OS_ANDROID) {
            // return jsb.reflection.callStaticMethod("com/common/ads/AdManage", "s_getPackageName", "()Ljava/lang/String;");
        }
        return 'com.apiter.diff.js'
    }

    static versionName = null;
    static getVersionName() {
        return ""
    }

    static getLevelIntervalAd(): number {
        console.log("NativeCaller getLevelIntervalAd");
        if (cc.sys.isNative && cc.sys.os == cc.sys.OS_ANDROID) {
        }
        return 2.0;
    }

    //native call to js

    static rewardSuccess(msg = ""): void {
        console.log("NativeCaller rewardSuccess");
        ServiceLogic.action(ActionType.ActionRewardDone, { 'beiz': msg || "" })
        if (StatusProperties.rewardType == RewardType.ReqTipInGameScene) {
            GameLogicController.getInstance().successShowHip();
            ServiceLogic.action(ActionType.ActionTipAdSuccess, {})
        }
        EventCenter.emit(EventNames.RewardedSuccess);
        //看了视频广告，将插屏显示初始化
        PlayerDataMgr.getInstance().notShowFullLevelCount = 0;
    }

    static rewardShowFailed(msg = ""): void {
        ToastHelper.show(i18n.t("noRewardAd") || "")
        EventCenter.emit(EventNames.RewardShowFailed)
        ServiceLogic.action(ActionType.ActionRewardFail, { 'beiz': msg || "" })
    }

    static interstitialShown(plat) {
        console.log("NativeCaller interstitialShown");
        ServiceLogic.action(ActionType.ActionFullAdDidShow, { 'beiz': plat || "admob" });
    }

    static onAdPaid(value: number = 0, type = 0) {
        console.log("IAD onAdPaid:" + value + ",type:" + type)
        PlayerDataMgr.getInstance().addAdRevenue(value);
        ServiceLogic.action(ActionType.ActionAdRevenue, { "level_name": type, "beiz": value, 'adRevenue': PlayerDataMgr.getInstance().totalAdRevenue })
    }

}
